/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.vdialog.model.page;

import java.net.URL;
import offset.nodes.Constants;
import offset.nodes.client.chooser.model.NewNode;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.dialog.newnode.model.AbstractNewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.NewNodeConfigurator;
import offset.nodes.client.dialog.newnode.model.ObjectType;
import offset.nodes.client.dialog.newnode.model.ServiceProcessor;
import offset.nodes.client.dialog.newnode.model.Step;
import offset.nodes.client.vdialog.view.node.NewNodeFromVirtualPagePanel;
import offset.nodes.client.vdialog.view.page.NewVirtualPagePanel;
import offset.nodes.client.model.HttpUtils;
import offset.nodes.client.model.ServerResponse;

/**
 *
 * @author Walter Lütgenau
 */
public class NewVirtualPageConfigurator extends AbstractNewNodeConfigurator implements NewNodeConfigurator {

    @Override
    public ObjectType getObjectType() {
        return new ObjectType(getBundle(), "objectType.virtualPage.name", "objectType.virtualPage.description",
                new Step[]{
                    new Step(getBundle(), "virtualPage.step1.name", new NewVirtualPagePanel(new ServerModel(getService())))}, new ServiceProcessor(getService(), getPath()) {

            public URL process() {
                URL result = null;

                try {
                    NewVirtualPagePanel newPagePanel = (NewVirtualPagePanel) getObjectType().getSteps()[1].getPanel();

                    CreateNewVirtualPage.Request request = new CreateNewVirtualPage.Request();

                    request.setPath(getPath());
                    request.setName(newPagePanel.getVirtualPageName());
                    request.setNodeType(newPagePanel.getNodeType());
                    ServerModel model = new ServerModel(getService());
                    CreateNewVirtualPage.Response response = (CreateNewVirtualPage.Response) model.sendRequest(request);

                    if (newPagePanel.doEdit() && response.getResult() == ServerResponse.RESULT_SUCCESS) {
                        String url = getRepository().toString() + request.getPath() + Constants.PATH_SEPARATOR + request.getName() + Constants.PATH_SEPARATOR + Constants.CHILD_INSTANCE;
                        url = HttpUtils.addParameter(url, Constants.PARVALUE_EDIT, Boolean.TRUE.toString().toLowerCase());

                        result = new URL(url);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                return result;
            }
        });

    }
}
